package com.jltx.xq.simplewatch.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.jltx.xq.simplewatch.R;


/**
 * 是否禁止viewpager的左右滑动的功能，默认是可以滚动
 * @author jltxseo
 *         Created by junlintianxia on 2017/06/26.
 */

public class SwipeableViewPager extends ViewPager {
    private boolean canScroll = true; //设置ViewPager是否可以滚动切换

    private boolean smoothScroll; //切换tab页面时是否平滑切换

    public SwipeableViewPager(Context context) {
        super(context);
        initView(context,null);
    }

    public SwipeableViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context,attrs);
    }

    private void initView(Context context, AttributeSet attrs){
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SwipeableViewPager);
        int n = a.getIndexCount();
        for(int i=0; i < n; i++){
            int attr = a.getIndex(i);
            switch (attr){
                case R.styleable.SwipeableViewPager_canScroll:
                    canScroll = a.getBoolean(attr,true);
                    break;
                case R.styleable.SwipeableViewPager_smoothScroll:
                    smoothScroll = a.getBoolean(attr,false);
                    break;
            }
        }
        a.recycle();
    }

    public boolean isCanScroll() {
        return canScroll;
    }

    public void setCanScroll(boolean canScroll) {
        this.canScroll = canScroll;
    }

    @Override
    public void setCurrentItem(int item) {
        setCurrentItem(item,smoothScroll);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return canScroll && super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return canScroll && super.onTouchEvent(ev);
    }
}
